<?php
// PC端 OA 客户管理
namespace Home\Controller;
use Think\Controller;
class ClientController extends CommonController {
	public function _initialize(){
		parent::_initialize();
	}

    public function index(){
        $p = I('get.p');
        $p = empty($p) ? 1 : $p;

        $search_type = I('param.search_type'); // 搜索条件 销售经理才有  3种情况：全部的、自己的、下属的 
        $date_begin  = I('param.date_begin'); // 搜索条件 开始日期
        $date_end    = I('param.date_end');   // 搜索条件 结束日期        
        if( !empty($date_begin) ){
            $map['add_time'] = array('EGT',strtotime($date_begin));
        }
        if( !empty($date_end) ){
            $map['add_time'] = array('ELT',strtotime($date_end));
        }
        
        $oaLevel = session('USER_AUTH.oa_level');  // 用户基本信息 session
        if($oaLevel == 1){  // 普通销售员 OA权限
            $map['uid'] = session('USER_AUTH.userid');
        }else if($oaLevel == 2){  // 销售经理 OA权限
            switch ($search_type) {  // 查询情况：1、所有的(自己和下属)；2、自己；3、下属
                case 'my': $map['uid'] = session('USER_AUTH.userid'); break;
                case 'xiashu': 
                    $uids = M('user')->where(array('oa_level'=>1))->getField('user_id',true);
                    $map['uid'] = array('in',$uids);
            }
        }

        $count = M('client')->where($map)->count();
        if($count){
            if($count == ($p-1)*10){ $p--; } // 删除记录时，判断当前页是否还有数据
            $clientList = M('client')->where($map)->field('id,uid,client_name,shop_manager,shop_birthday,location,add_time')->page($p.',10')->order('id desc')->select();
            $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
            $pageStyle = C('PAGE_STYLE');  // 获取分页样式主题
            $Page ->setConfig('prev','上一页');
            $Page ->setConfig('next','下一页');
            $Page ->setConfig('theme',$pageStyle);
            $Page ->parameter['search_type'] = $search_type;  // 分页链接 传值 销售经理才有  查询情况
            $Page ->parameter['date_begin']  = $date_begin;   // 分页链接 传值 开始日期
            $Page ->parameter['date_end']    = $date_end;     // 分页链接 传值 结束日期
            $show = $Page->show();// 分页显示输出

            foreach($clientList as $k => $val){
                $userIds = empty($userIds) ? $val['uid'] : $userIds.','.$val['uid']; // 获取用户id，用于用户表 查询 用户相关内容
            }
            $userList = get_users_list($userIds);
            foreach($clientList as $k => $val){
                foreach($userList as $vo){
                    if($vo['user_id'] == $val['uid']){
                        $clientList[$k]['add_name'] = $vo['user_name'];
                    }
                }
            }
        }else{
            $clientList = array();
        }

        $this->assign('empty','<tr class="text-c odd" role="row"><td colspan="9">暂无数据</td></tr>');
        $this->assign('clientList',$clientList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('search_type',$search_type);  // 赋值 搜索条件 销售经理才有  查询情况
        $this->assign('date_begin',$date_begin);    // 赋值 搜索条件 开始日期
        $this->assign('date_end',$date_end);        // 赋值 搜索条件 结束日期
        $this->assign('p',$p);
        $this->display();
    }

    // 客户详情页
    public function detail(){
        $id = I('get.id');
        $map['id'] = $id;
        $clientArr = M('client')->where($map)->find();
        $contactList = M('contact')->where(array('cid'=>$id))->field('id,realname')->select();
        $this->assign('clientArr',$clientArr);
        $this->assign('contactList',$contactList);
        $this->display();
    }

    // 负责人详情页
    public function contactDetail(){
        $id = I('get.id');
        $map['id'] = $id;
        $contactArr = M('contact')->where($map)->find();
        $this->assign('contactArr',$contactArr);
        $this->display();

    }

    // 删除（单个/批量）
    public function del(){
        $id = I('get.id');
        $p  = I('get.p'); 
        if($id){
            $rel1 = M('client')->where(array('id'=>$id))->delete();
            $rel2 = M('contact')->where(array('cid'=>$id))->delete();
            $this->redirect('Client/index',array('p'=>$p));
        }
    }

    // 客户添加页
    public function add(){
        $id = I('get.cid');  // 添加负责人 添加成功后 传此客户id
        if($id){ // 有则表示此用户已添加负责人，即此客户记录已添加到数据库
            $clientArr = M('client')->where(array('id'=>$id))->find();
            $contactList = M('contact')->where(array('cid'=>$id))->field('id,realname')->select();
            $this->assign('clientArr',$clientArr);
            $this->assign('contactList',$contactList);
            $this->assign('id',$id);
        }
        //p($clientArr);
        $this->display();
    }
    
    // 选择客户地点
    public function choiceAddress(){
        $id = I('get.id');
        $this->assign('id',$id);
        $this->display();
    }

    // 选择地点 提交
    public function choiceAddressCheck(){
        $map['id'] = I('post.id');
        if($map['id']){
            $map['location']  = I('post.location');
            $map['longitude'] = I('post.longitude');
            $map['latitude'] = I('post.latitude');
            $rel = M('client')->save($map);
            if($rel){
                $msg = array('state'=>200,'content'=>'选择成功');  
            }else{
                $msg = array('state'=>500,'content'=>'选择失败，请重新选择');  
            }
        }else{
            $msg = array('state'=>500,'content'=>'选择失败，请重新选择'); 
        }
        $this->ajaxReturn($msg);
    }

    // 添加客户 提交  
    public function addCheck(){
        $id = I('get.id');  // 由添加负责人按钮 触发 ajax get传值过来
        if(!$id){  // 如果没有  两种情况：1、说明未添加负责人，直接添加客户；2、第一次添加负责人
            $client_name       = I('post.client_name');
            $shop_manager      = I('post.shop_manager');
            $shop_birthday     = I('post.shop_birthday');
            $visit_year_num    = I('post.visit_year_num');
            $visit_quarter_num = I('post.visit_quarter_num');
            $visit_month_num   = I('post.visit_month_num');
            $remark            = I('post.remark');
            $location          = I('post.location');
            $longitude         = I('post.longitude');
            $latitude          = I('post.latitude');
            $tel               = I('post.tel');
            $qq                = I('post.qq');
            $wechat            = I('post.wechat');
            $email             = I('post.email');

            $data['uid'] = session('USER_AUTH.userid');
            $data['client_name']       = $client_name;
            $data['shop_manager']      = $shop_manager;
            $data['shop_birthday']     = $shop_birthday ? strtotime($shop_birthday) : '';
            $data['visit_year_num']    = $visit_year_num;
            $data['visit_quarter_num'] = $visit_quarter_num;
            $data['visit_month_num']   = $visit_month_num;
            $data['remark']            = $remark;
            $data['location']          = $location;
            $data['longitude']         = $longitude;
            $data['latitude']          = $latitude;
            $data['tel']               = $tel;
            $data['qq']                = $qq;
            $data['wechat']            = $wechat;
            $data['email']             = $email;
            $data['add_time']          = time();

            $rel = M('client')->add($data);
            if($rel){
                $msg = array('state'=>200,'id'=>$rel,'content'=>'添加成功');
            }else{
                $msg = array('state'=>500,'content'=>'添加失败');
            }
        }else{  // 2种情况：1、再次添加负责人； 2、提交客户信息，修改
            $client_name       = I('post.client_name');
            $shop_manager      = I('post.shop_manager');
            $shop_birthday     = I('post.shop_birthday');
            $visit_year_num    = I('post.visit_year_num');
            $visit_quarter_num = I('post.visit_quarter_num');
            $visit_month_num   = I('post.visit_month_num');
            $remark            = I('post.remark');
            $location          = I('post.location');
            $longitude         = I('post.longitude');
            $latitude          = I('post.latitude');
            $tel               = I('post.tel');
            $qq                = I('post.qq');
            $wechat            = I('post.wechat');
            $email             = I('post.email');

            $data['id']  = $id;
            $data['client_name']       = $client_name;
            $data['shop_manager']      = $shop_manager;
            $data['shop_birthday']     = $shop_birthday ? strtotime($shop_birthday) : '';
            $data['visit_year_num']    = $visit_year_num;
            $data['visit_quarter_num'] = $visit_quarter_num;
            $data['visit_month_num']   = $visit_month_num;
            $data['remark']            = $remark;
            $data['location']          = $location;
            $data['longitude']         = $longitude;
            $data['latitude']          = $latitude;
            $data['tel']               = $tel;
            $data['qq']                = $qq;
            $data['wechat']            = $wechat;
            $data['email']             = $email;

            $rel = M('client')->save($data);
            $msg = array('state'=>200,'id'=>$id,'content'=>'添加成功');
            // if($rel){
            //     $msg = array('state'=>200,'id'=>$id,'content'=>'添加成功');
            // }else{
            //     $msg = array('state'=>500,'id'=>$id,'content'=>'添加失败');
            // }
            
        }
        
        $this->ajaxReturn($msg);

    }

    // 负责人添加页
    public function contactAdd(){
        $cid = I('get.cid');  // 已添加的客户id
        $this->assign('cid',$cid);
        $this->display();
    }

    // 添加负责人 提交
    public function contactAddCheck(){
        $cid = I('get.cid');  // 已添加的客户id

        $realname = I('post.realname');
        $birthday = I('post.birthday');
        $sex      = I('post.sex');
        $tel      = I('post.tel');
        $qq       = I('post.qq');
        $wechat   = I('post.wechat');
        $email    = I('post.email');

        $data['uid']      = session('USER_AUTH.userid');  // 添加人id 用户id
        $data['cid']      = $cid;
        $data['realname'] = $realname;
        $data['birthday'] = $birthday ? strtotime($birthday) : '';
        $data['sex']      = $sex;
        $data['tel']      = $tel;
        $data['qq']       = $qq;
        $data['wechat']   = $wechat;
        $data['email']    = $email;
        $data['add_time'] = time();

        $rel = M('contact')->add($data);

        if($rel){
            $msg = array('state'=>200,'content'=>'添加成功');
        }else{
            $msg = array('state'=>500,'content'=>'添加失败');
        }

        $this->ajaxReturn($msg);

    }

    public function quxiao(){
        $id = I('get.id');
        $map1['id'] = $id;
        $map2['cid'] = $id;
        $rel1 = M('client')->where($map1)->delete();
        $rel2 = M('contact')->where($map2)->delete();
        if($rel1 || $rel2){
            $msg = array('state'=>200,'content'=>'取消成功');
        }else{
            $msg = array('state'=>500,'content'=>'取消失败');
        }
        $this->ajaxReturn($msg);
    }

    // 客户修改页
    public function edit(){
        $id = I('get.id');  
        $clientArr = M('client')->where(array('id'=>$id))->find();
        $contactList = M('contact')->where(array('cid'=>$id))->field('id,realname')->select();
        $this->assign('clientArr',$clientArr);
        $this->assign('contactList',$contactList);

        $this->display();
    }

    // 客户修改提交
    public function editCheck(){
        $id = I('get.id');
        $client_name       = I('post.client_name');
        $shop_manager      = I('post.shop_manager');
        $shop_birthday     = I('post.shop_birthday');
        $visit_year_num    = I('post.visit_year_num');
        $visit_quarter_num = I('post.visit_quarter_num');
        $visit_month_num   = I('post.visit_month_num');
        $remark            = I('post.remark');
        $location          = I('post.location');
        $longitude         = I('post.longitude');
        $latitude          = I('post.latitude');
        $tel               = I('post.tel');
        $qq                = I('post.qq');
        $wechat            = I('post.wechat');
        $email             = I('post.email');

        $data['id']  = $id;
        $data['client_name']       = $client_name;
        $data['shop_manager']      = $shop_manager;
        $data['shop_birthday']     = $shop_birthday ? strtotime($shop_birthday) : '';
        $data['visit_year_num']    = $visit_year_num;
        $data['visit_quarter_num'] = $visit_quarter_num;
        $data['visit_month_num']   = $visit_month_num;
        $data['remark']            = $remark;
        $data['location']          = $location;
        $data['longitude']         = $longitude;
        $data['latitude']          = $latitude;
        $data['tel']               = $tel;
        $data['qq']                = $qq;
        $data['wechat']            = $wechat;
        $data['email']             = $email;

        $rel = M('client')->save($data);
        if($rel){
            $msg = array('state'=>200,'id'=>$id,'content'=>'修改成功');
        }else{
            $msg = array('state'=>500,'id'=>$id,'content'=>'修改失败');
        }
        $this->ajaxReturn($msg);

    }

    // 负责人修改页
    public function contactEdit(){
        $id = I('get.id');
        $contactArr = M('contact')->where(array('id'=>$id))->find();
        $contactArr['birthday'] = $contactArr['birthday'] ? date('Y-m-d',$contactArr['birthday']) : '';
        
        $this->assign('contactArr',$contactArr);
        //p($contactArr);die;
        $this->display();
    }

    // 负责人删除
    public function contactDel(){
        $id = I('get.id');
        $cid = I('get.cid');
        $rel = M('contact')->where(array('id'=>$id))->delete();
        $this->redirect('Client/edit',array('id'=>$cid));
    }

    // 负责人修改 提交
    public function contactEditCheck(){
        $id = I('get.id');

        $realname = I('post.realname');
        $birthday = I('post.birthday');
        $sex      = I('post.sex');
        $tel      = I('post.tel');
        $qq       = I('post.qq');
        $wechat   = I('post.wechat');
        $email    = I('post.email');

        $data['id'] = $id;
        $data['realname'] = $realname;
        $data['birthday'] = $birthday ? strtotime($birthday) : '';
        $data['sex']      = $sex;
        $data['tel']      = $tel;
        $data['qq']       = $qq;
        $data['wechat']   = $wechat;
        $data['email']    = $email;

        $rel = M('contact')->save($data);
        if($rel){
            $msg = array('state'=>200,'content'=>'修改成功');
        }else{
            $msg = array('state'=>500,'content'=>'修改失败');
        }

        $this->ajaxReturn($msg);
    }

    // 修改页中的添加负责人页面
    public function editContactAdd(){
        $cid = I('get.cid');
        $this->assign('cid',$cid);
        $this->display();
    }



}